<?php
namespace Zxf5115\Laravel\Local\Upload\Utils;

use Illuminate\Support\Facades\Config;

/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2025-09-08
 *
 * 资源上传类型工具类
 */
class FileTypeUtil
{
  /**
   * 允许的上传类型枚举映射
   */
  private const UPLOAD_TYPE_CONFIG_MAP = [
    'file' => 'upload.allowed_file_extension',
    'picture' => 'upload.allowed_picture_extension',
    'rich_text' => 'upload.allowed_rich_text_extension',
  ];


  /**
   * 根据上传类型获取允许的文件类型
   *
   * @param string $uploadType 上传类型
   * @param array $customAllow 自定义允许类型
   * @return array 最终允许的类型数组
   */
  public static function getAllowTypesByUploadType(string $uploadType, array $customAllow) : array
  {
    // 自定义类型优先，否则从配置获取
    if(is_array($customAllow) && !empty($customAllow))
    {
      return $customAllow;
    }

    return Config::get(self::UPLOAD_TYPE_CONFIG_MAP[$uploadType], ['jpg', 'png', 'gif']);
  }
}
